package com.caigou.service.impl;

import com.caigou.bean.cms.entity.DepartmentDictionary;
import com.caigou.bean.cms.entity.Privilege;
import com.caigou.bean.cms.entity.Role;
import com.caigou.bean.vo.KeyValueVO;
import com.caigou.mapper.cms.DepartmentDictionaryMapper;
import com.caigou.mapper.cms.PrivilegeMapper;
import com.caigou.mapper.cms.RoleMapper;
import com.caigou.service.IDictionaryService;
import com.caigou.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DictionaryServiceImpl implements IDictionaryService {

    @Resource
    private DepartmentDictionaryMapper departmentDictionaryMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PrivilegeMapper privilegeMapper;

    @Override
    public Result<List<KeyValueVO>> findDepartments() {
        List<DepartmentDictionary> departments = departmentDictionaryMapper.findDepartments();
        List<KeyValueVO> valueVOS = departments.stream()
                .collect(Collectors.toMap(DepartmentDictionary::getDepartmentCode, DepartmentDictionary::getDepartmentName))
                .entrySet().stream().sorted(Map.Entry.comparingByKey())
                .map(dic -> new KeyValueVO( dic.getValue(),dic.getKey()))
                .collect(Collectors.toList());
        return Result.success(valueVOS);
    }

    @Override
    public Result<List<KeyValueVO>> findRoles() {

        List<Role> roles = roleMapper.findAllRoles();
        List<KeyValueVO> valueVOS = roles.stream()
                .collect(Collectors.toMap(Role::getRoleCode, Role::getRoleName))
                .entrySet().stream().sorted(Map.Entry.comparingByKey())
                .map(dic -> new KeyValueVO( dic.getValue(),dic.getKey()))
                .collect(Collectors.toList());
        return Result.success(valueVOS);
    }

    @Override
    public Result<List<KeyValueVO>> findPrivileges() {
        List<Privilege> privileges = privilegeMapper.findAllPrivileges();
        List<KeyValueVO> valueVOS = privileges.stream()
                .collect(Collectors.toMap(Privilege::getPrivilegeCode, Privilege::getPrivilegeName))
                .entrySet().stream().sorted(Map.Entry.comparingByKey())
                .map(dic -> new KeyValueVO(dic.getValue(),dic.getKey()))
                .collect(Collectors.toList());
        return Result.success(valueVOS);
    }
}
